package com.mujin.keji.collectionsystem.module.message.views;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mujin.keji.collectionsystem.CcsApplication;
import com.mujin.keji.collectionsystem.R;
import com.mujin.keji.collectionsystem.base.BaseActivity;
import com.mujin.keji.collectionsystem.base.BaseDto;
import com.mujin.keji.collectionsystem.listeners.CustomItemClickListener;
import com.mujin.keji.collectionsystem.module.message.adapter.MessageAdapter;
import com.mujin.keji.collectionsystem.module.message.dto.MsgDetail;
import com.mujin.keji.collectionsystem.module.message.dto.MsgDto;
import com.mujin.keji.collectionsystem.module.message.viewmodel.MsgViewModel;
import com.mujin.keji.collectionsystem.module.outboundtask.views.AssimilatorActivity;
import com.mujin.keji.collectionsystem.module.outboundtask.views.OutBoundtaskListActivity;
import com.mujin.keji.collectionsystem.module.updata.views.UpLoadAllUserDataActivity;
import com.mujin.keji.collectionsystem.utils.DisplayUtil;
import com.mujin.keji.collectionsystem.utils.EmptyUtils;
import com.mujin.keji.collectionsystem.widget.CustomerItemDecoration;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 消息列表Activity界面
 *
 * @author weishixiong
 * @Time 2018-03-22
 */

public class MessageActivity extends BaseActivity implements CustomItemClickListener {
    private MessageAdapter messageAdapter;
    private LinearLayoutManager linearLayoutManager;
    private SmartRefreshLayout srHasData;
    private RecyclerView rvMsg;
    private Class[] classes = new Class[]{null, UpLoadAllUserDataActivity.class, AssimilatorActivity.class, OutBoundtaskListActivity.class, AssimilatorActivity.class};
    private List<MsgDetail> msgDetailList;
    private MsgViewModel msgViewModel;
    private LinearLayout llNoData;
    private SmartRefreshLayout srNoData;
    private TextView tvNoData;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CcsApplication.addActivity(this);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CcsApplication.removeActivity(this);

    }

    @Override
    protected void createViewModel() {
        msgViewModel = ViewModelProviders.of(this).get(MsgViewModel.class);
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_message;
    }

    @Override
    public void initView() {
        setCenterTitle("消息通知", "#FFFFFF");
        setleftImage(R.drawable.image_arrow_left_white, true, null);
        srHasData = (SmartRefreshLayout) findViewById(R.id.refreshLayout);
        rvMsg = (RecyclerView) findViewById(R.id.rv_msg);
        llNoData = (LinearLayout) findViewById(R.id.ll_no_data);
        srNoData = (SmartRefreshLayout) findViewById(R.id.sr_no_data);
        tvNoData = (TextView) findViewById(R.id.tv_no_data);
        rvMsg.addItemDecoration(new CustomerItemDecoration(DisplayUtil.dip2px(this, 10)));
        msgDetailList = new ArrayList<>();
        linearLayoutManager = new LinearLayoutManager(this);
        rvMsg.setLayoutManager(linearLayoutManager);
        messageAdapter = new MessageAdapter(this, this.msgDetailList, this);
        rvMsg.setAdapter(messageAdapter);
        srNoData.setEnableLoadMore(false);
    }


    @Override
    public void initData() {
        srHasData.autoRefresh();
    }


    private int page = 1;

    private void loadData() {
        msgViewModel.getMsgRefreshLiveData(page).observe(this, new Observer<BaseDto<MsgDto>>() {
            @Override
            public void onChanged(@Nullable BaseDto<MsgDto> msgDtoBaseDto) {
                getLoadingDialog().dismiss();
                srNoData.finishRefresh();
                srHasData.finishRefresh();
                srHasData.finishLoadMore();
                if (EmptyUtils.isNotEmpty(msgDtoBaseDto.getData()) && EmptyUtils.isNotEmpty(msgDtoBaseDto.getData().getItems())) {
                    llNoData.setVisibility(View.GONE);
                    srHasData.setVisibility(View.VISIBLE);
                    if (page == 1) {
                        msgDetailList.clear();
                    }
                    msgDetailList.addAll(msgDtoBaseDto.getData().getItems());
                    messageAdapter.notifyDataSetChanged();
                } else {
                    //说明是上拉加载
                    if (page > 1) {
                        page--;
                    } else {
                        //第一次就没数据
                        msgDetailList.clear();
                        messageAdapter.notifyDataSetChanged();
                        llNoData.setVisibility(View.VISIBLE);
                        srHasData.setVisibility(View.GONE);
                        tvNoData.setText("暂无消息通知");
                    }
                }


            }
        });
    }

    @Override
    public void initListener() {
        srHasData.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                loadData();
            }
        });
        srHasData.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                loadData();
            }
        });
        srNoData.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                loadData();
            }
        });

    }

    @Override
    public void onItemClick(View view, int position) {
        Intent intent = new Intent(this, classes[this.msgDetailList.get(position).getType()]);
        if (3 == this.msgDetailList.get(position).getType()) {
            intent.putExtra("status", 4);
        } else {
            intent.putExtra("status", 5);
        }
        startActivity(intent);
    }
}
